package com.summer.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class FutureTaskTest {
	
	public static void main(String[] args) {
		FutureTaskTest task = new FutureTaskTest();
		
//		String msg = task.getRemoteData();
		Future<String> future = task.getFutureRemoteData();
		
		System.out.println("准备数据");
		
//		System.out.println(msg);
		try {
			String msg = future.get();
			System.out.println(msg);
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		} 
	}
	
	public String getRemoteData(){
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "获取成功";
	}
	
	public Future<String> getFutureRemoteData(){
		FutureTask<String> future = new FutureTask<String>(new Callable<String>(){
			@Override
			public String call() throws Exception {
				return getRemoteData();
			}});
		new Thread(future).start();
		return future;
	}
}
